package com.fashion.model.gallery;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.struts2.json.annotations.JSON;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.OrderBy;

import com.fashion.utils.StringUtils;
import com.fashion.utils.UrlKeyword;

@Entity
@Table(name="FASHION.GA_ALBUM")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Album implements Serializable{
	@OneToMany(mappedBy = "album", fetch=FetchType.LAZY)
	@OrderBy(clause = "date desc, name asc")
	private List<Photo> photoList = new ArrayList<Photo>();
	@Column(name="TITLE")
	private String title;
	@Column(name="VICETITLE")
	private String viceTitle;
	@Column(name="KEYWORDS")
	private String keywords;
	@Transient
	private List<UrlKeyword> urlKeywords;
	@Column(name="CLICK_NUMBER")
	private int clickNumber;
	@Column(name="DESCRIPTION")
	private String description;
	@Id 
	@Column(name="ID", updatable=false)
	private String id;
	@Column(name="OWNER")
	private String owner;
	@Column(name="COVER")
	private String cover;
	@Column(name="PRIORITY")
    private int priority;
	@Column(name="DATE")
	private Date date; 
	@ManyToOne(targetEntity=GalleryColumn.class, fetch=FetchType.EAGER)
	@JoinColumn(name="COLUMN_ID")
	private GalleryColumn column;
	
	@JSON(serialize = false)
	public List<Photo> getPhotoList() {
		return photoList;
	}

	public void setPhotoList(List<Photo> photoList) {
		this.photoList = photoList;
	}	
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}
	
	public GalleryColumn getColumn() {
		return column;
	}

	public void setColumn(GalleryColumn column) {
		this.column = column;
	}

	public String getCover() {
		return cover;
	}

	public void setCover(String cover) {
		this.cover = cover;
	}

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

	public String getViceTitle() {
		return viceTitle;
	}

	public void setViceTitle(String viceTitle) {
		this.viceTitle = viceTitle;
	}

	public int getClickNumber() {
		return clickNumber;
	}

	public void setClickNumber(int clickNumber) {
		this.clickNumber = clickNumber;
	}
	
	public List<UrlKeyword> getKeywordList() throws UnsupportedEncodingException {
        if (StringUtils.isNullOrEmpty(keywords)) {
            return new ArrayList<UrlKeyword>();
        }

        if(null != urlKeywords){
            return urlKeywords;
        }

        urlKeywords = new ArrayList<UrlKeyword>();
        List<String> rawKeywords = StringUtils.splitToList(keywords, " ");
        for (String keyword : rawKeywords) {
            String keywordWithoutSpace = keyword.replace("$", " ");
            /*urlKeywords.add(new UrlKeyword(keywordWithoutSpace, URLEncoder.encode(keywordWithoutSpace, "UTF-8")));*/
            urlKeywords.add(StringUtils.makeUrlKeyword(keywordWithoutSpace));
        }

        return urlKeywords;
    }

}
